package com.ymxk.timestar.activity;

import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ymxk.timestar.view.drag.utils.AnimationUtils;

import java.util.ArrayList;

public class LocationInViewGroupActivity extends AppCompatActivity {

    private Button mBtnMove;
    private TextView mTvCenter;
    private final String TAG = LocationInWindowActivity.class.getSimpleName();
    private Button mBtnGo;
    private LinearLayout mLlBtn;
    private Button mBtnMove1;
    private Button mBtnGo1;
    private Button mBtnGo2;
    private Button mBtnGo3;
    private int barHeight;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, LocationInViewGroupActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        barHeight = getStatusBarHeight();
        Log.e(TAG, "onCreate: barHeight--" + barHeight);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_location_in_view_group);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            Log.e(TAG, "onCreate: systemBars.top--" + systemBars.top);
            Log.e(TAG, "onCreate: systemBars.bottom--" + systemBars.bottom);
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();

        mTvCenter.post(this::onInitCenterLocation);

    }

    private void onInitCenterLocation() {
        int width = mTvCenter.getWidth();
        int height = mTvCenter.getHeight();

        int x = mTvCenter.getLeft() - 600*2;
        int y = mTvCenter.getTop();

        PointF pointF = new PointF(x, y);
        PointF pointF1 = new PointF(x + width, y);
        PointF pointF2 = new PointF(x, y + height);
        PointF pointF3 = new PointF(x + width, y + height);

        list.clear();
        list.add(pointF);
        list.add(pointF1);
        list.add(pointF2);
        list.add(pointF3);

    }

    ArrayList<PointF> list = new ArrayList<>();

    private void initView() {
        mBtnMove = findViewById(R.id.btnMove);
        mBtnMove1 = findViewById(R.id.btnMove1);
        mTvCenter = findViewById(R.id.tvCenter);

        mTvCenter.setOnClickListener(v -> onInitCenterLocation());

        mBtnGo = findViewById(R.id.btnGo);

        mLlBtn = findViewById(R.id.llBtn);
        mBtnGo1 = findViewById(R.id.btnGo1);
        mBtnGo2 = findViewById(R.id.btnGo2);
        mBtnGo3 = findViewById(R.id.btnGo3);

        mBtnGo.setOnClickListener(v -> translation(list.get(0)));
        mBtnGo1.setOnClickListener(v -> translation(list.get(1)));
        mBtnGo2.setOnClickListener(v -> translation(list.get(2)));
        mBtnGo3.setOnClickListener(v -> translation(list.get(3)));

    }

    private void translation(PointF pointF) {
        //.translationY(pointF.y - barHeight)
        mLlBtn.animate()
                .translationX(pointF.x)
                .translationY(pointF.y)
                .setListener(new AnimationUtils.AnimationEndListener() {
                    @Override

                    public void onAnimationEnd(@NonNull Animator animation) {
                        Log.e(TAG, "onAnimationEnd: ");
                    }
                })
                .start();

    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}